﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using DAL.Engine.Interface;
using System.Linq;
using System;
using DAL.Engine.Domain;
using DAL.Engine.Specification;

namespace DAL.Mongo.Repository
{
    [TestClass]
    public class UserRepositoryTest : BaseTest
    {
        private IUserRepository _userRepository;
        private Guid _userId = Guid.NewGuid();

        [TestInitialize]
        public void Setup()
        {
            _userRepository = Container.GetInstance<IUserRepository>();
            _userRepository.Save(new User { Id = _userId, Name = "user1", DayOfBirth = DateTime.UtcNow.AddYears(-10) });
            _userRepository.Save(new User { Id = Guid.NewGuid(), Name = "user2", DayOfBirth = DateTime.UtcNow.AddYears(-20) });
            _userRepository.Save(new User { Id = Guid.NewGuid(), Name = "user3", DayOfBirth = DateTime.UtcNow.AddYears(-30) });
        }

        [TestCleanup]
        public void Cleanup()
        {
            _userRepository.DeleteAll();
        }

        [TestMethod]
        public void TestGetAll()
        {
            // Act            
            var count = _userRepository.GetAll().Count();

            // Assert
            Assert.IsTrue(count == 3);
        }

        [TestMethod]
        public void TestGetById()
        {
            // Act            
            var user = _userRepository.Get(_userId);

            // Assert
            Assert.IsNotNull(user);
        }

        [TestMethod]
        public void TestUpdate()
        {
            // Act            
            var user1 = _userRepository.Get(_userId);
            user1.Name = "new_user";

            _userRepository.Save(user1);

            var user2 = _userRepository.Get(_userId);

            // Assert
            Assert.IsFalse(user1.IsNew);
            Assert.IsFalse(user2.IsNew);
            Assert.AreEqual(user1, user2);
        }

        [TestMethod]
        public void TestDeleteById()
        {
            // Act            
            _userRepository.Delete(_userId);
            var user = _userRepository.Get(_userId);

            // Assert
            Assert.IsNull(user);
        }

        [TestMethod]
        public void TestDeleteAll()
        {
            // Act            
            _userRepository.DeleteAll();
            var count = _userRepository.GetAll().Count();

            // Assert
            Assert.AreEqual(count, 0);
        }
    }
}
